home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / citsrc6K05.lha / clean.c < prev    next >
C/C++ Source or Header  |  1996-10-13  |  6KB  |  280 lines

  1. /*
  2.  *                              Clean.c
  3.  *
  4.  * Cleanses a room of a user.
  5.  */
  6.  
  7. #include "ctdl.h"    /* header file  */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <ctype.h>
  13. #include <time.h>
  14. #include <proto/exec.h>
  15. #include <dos/dos.h>
  16. #include <pragmas/dos_pragmas.h>
  17. #include "exec/memory.h"
  18. #include "exec/ports.h"
  19. #include "exec/exec.h"
  20.  
  21. /*
  22.  *                              History
  23.  *
  24.  * 88Jun23 HAW  Created
  25.  */
  26.  
  27. /*
  28.  *                              Contents
  29.  *
  30.  *      crashout()              irrecoverable error
  31.  *      doRest()                Read msgs.
  32.  *      handle()                Inserts msg into room slot, etc.
  33.  *      indexRooms()            build RAM index to ctdlroom.sys
  34.  *      init()                  Open up files for recovery effort.
  35.  *      main()                  Master control.
  36.  *      noteRoom()              enter room into RAM index
  37.  */
  38.  
  39. extern CONFIG  cfg;             /* The main variable to be saved */
  40. extern MessageBuffer msgBuf;    /* The -sole- message buffer */
  41. extern FILE    *msgfl, *msgfl2; /* file descriptor for the msg file */
  42. extern rTable  *roomTab;        /* RAM index of rooms */
  43. extern aRoom   roomBuf;         /* room buffer */
  44. extern FILE    *roomfl;         /* file descriptor for rooms    */
  45. extern int     thisRoom;        /* room currently in roomBuf    */
  46.  
  47. struct bad {
  48.     label name;
  49.     struct bad *next;
  50. } base;
  51.  
  52. void handle(MSG_NUMBER mess);
  53. void getUtilString(char *prompt, char *buf, int lim);
  54. char init(int argc, char **argv);
  55. void doMsgs(void);
  56. int UtilGetch(void);
  57. int  mPrintf(char *format, ...) {return 0; }  /* stub to quiet the linker */
  58.  
  59. /*
  60.  * crashout()
  61.  *
  62.  * Irrecoverable error?  Crash out of the program.
  63.  */
  64. void crashout(str)
  65. char *str;
  66. {
  67.     exit(printf(str));
  68. }
  69.  
  70. /*
  71.  * doRest()
  72.  *
  73.  * This loops thru msg file until finished.
  74.  */
  75. void doMsgs()
  76. {
  77.     MSG_NUMBER message, firstMessage;
  78.     extern struct mBuf mFile1;
  79.  
  80.     startAt(msgfl, &mFile1, 0, 0);
  81.     getMessage(getMsgChar, FALSE, FALSE, TRUE);
  82.     message = atol(msgBuf.mbId);
  83.     printf("%ld\n", message);
  84.     firstMessage = message;
  85.     handle(message);
  86.     getMessage(getMsgChar, FALSE, FALSE, TRUE);
  87.     message = atol(msgBuf.mbId);
  88.     while (message != firstMessage) {
  89.         printf("%ld\n", message);
  90.         handle(message);
  91.         getMessage(getMsgChar, FALSE, FALSE, TRUE);
  92.         message = atol(msgBuf.mbId);
  93.     }
  94. }
  95.  
  96. /*
  97.  * handle()
  98.  *
  99.  * This function handles setting up and saving room indexes, etc.
  100.  */
  101. void handle(mess)
  102. MSG_NUMBER mess;
  103. {
  104.     int k, j;
  105.     struct bad *cur;
  106.  
  107.     if (strCmpU(msgBuf.mbroom, roomBuf.rbname) != SAMESTRING) return;
  108.     for (cur = base.next; cur != NULL; cur = cur->next)
  109.         if (strCmpU(cur->name, msgBuf.mbauth) == SAMESTRING) return;
  110.  
  111.     printf("Message from %s\n", msgBuf.mbauth);
  112.  
  113.     for (k = 0; roomBuf.msg[k+1].rbmsgNo < mess && k < MSGSPERRM-1; k++)
  114.         ;
  115.     for (j = 0; j < k; j++) {       /* Move msgs up 1.      */
  116.         roomBuf.msg[j].rbmsgLoc = roomBuf.msg[j+1].rbmsgLoc;
  117.         roomBuf.msg[j].rbmsgNo  = roomBuf.msg[j+1].rbmsgNo;
  118.     }
  119.     roomBuf.msg[k].rbmsgNo  = mess;/* And now insert msg    */
  120.     roomBuf.msg[k].rbmsgLoc = msgBuf.mbheadSector;
  121. }
  122.  
  123. /*
  124.  * init()
  125.  *
  126.  * This will set up the files for recovery.
  127.  */
  128. char init(argc, argv)
  129. int argc;
  130. char *argv[];
  131. {
  132.     label RoomName, UserName;
  133.     int  roomNo, count = 0, i;
  134.     SYS_FILE temp;
  135.     struct bad *cur;
  136.  
  137.     cfg.weAre = UTILITY;
  138.     if (!readSysTab(TRUE, TRUE)) crashout("\nDisaster!  I need CTDLTABL.SYS!");
  139.  
  140.     if (access(LOCKFILE, 0) != -1) {
  141.         printf("Please do not run Clean from Outside Commands.\n");
  142.         return FALSE;
  143.     }
  144.  
  145.     cfg.weAre = UTILITY;
  146.     mvToHomeDisk(&cfg.homeArea);
  147.     /* open message file */
  148.  
  149.     InitMsgBase();
  150.     makeSysName(temp, "ctdlroom.sys", &cfg.roomArea);
  151.     /* open room file */
  152.     openFile(temp, &roomfl);
  153.  
  154.     initRoomBuf(&roomBuf);
  155.  
  156.     if (argc != 2) {
  157.         getUtilString("room name", RoomName, NAMESIZE);
  158.     }
  159.     else strCpy(RoomName, argv[1]);
  160.  
  161.     printf("Check for room %s\n", RoomName);
  162.  
  163.     if ((roomNo = roomExists(RoomName)) == ERROR) crashout("No such room!");
  164.  
  165.     getRoom(roomNo);
  166.  
  167.     for (i = 0; i < MSGSPERRM; i++) {
  168.         roomBuf.msg[i].rbmsgNo = 0l;
  169.         roomBuf.msg[i].rbmsgLoc = 0;
  170.     }
  171.  
  172.     cur = &base;
  173.  
  174.     printf("Type in bad users names, end with no name:\n");
  175.     do {
  176.         getUtilString("", UserName, NAMESIZE);
  177.         if (strLen(UserName) != 0) {
  178.             count++;
  179.             cur->next = GetDynamic(sizeof *cur);
  180.             cur = cur->next;
  181.             strCpy(cur->name, UserName);
  182.             cur->next = NULL;
  183.         }
  184.     } while (strLen(UserName) != 0);
  185.  
  186.     if (count == 0) {
  187.         printf("No users specified!\n");
  188.         return FALSE;
  189.     }
  190.     return TRUE;
  191. }
  192.  
  193. /*
  194.  * main()
  195.  *
  196.  * This directs salvage proceedings and collects profits.
  197.  */
  198. int main(int, char **);
  199. int main(argc, argv)
  200. int argc;
  201. char **argv;
  202. {
  203.     printf("Citadel CLEAN a room %s\n%s\n", VERSION_NAME, COPYRIGHT);
  204.     if (init(argc, argv)) {
  205.         puts("No error msgs.  Go off and do something for an hour (or 2).\n");
  206.         doMsgs();
  207.         putRoom(thisRoom);
  208.         printf("Don't need to reconfigure.\n");
  209.     }
  210.     writeSysTab();
  211.     return 0;
  212. }
  213.  
  214. /*
  215.  * getUtilString()
  216.  *
  217.  * This function gets a string from the user.
  218.  */
  219. void getUtilString(prompt, buf, lim)
  220. char *prompt;
  221. char *buf;
  222. int  lim;       /* max # chars to read */
  223. {
  224.     char c;
  225.     int  i;
  226.  
  227.     if(strLen(prompt) > 0) {
  228.         printf("Enter %s\n : ", prompt, lim);
  229.     }
  230.  
  231.     i   = 0;
  232.     while (
  233.         c = UtilGetch(),
  234.  
  235.         c       != 13
  236.         && i     <  lim
  237.     ) {
  238.  
  239.         /* handle delete chars: */
  240.         if (c == BACKSPACE) {
  241.             putchar(' ');
  242.             putchar(BACKSPACE);
  243.             if (i > 0) i--;
  244.             else  {
  245.                 putchar(' ');
  246.                 putchar(BELL);
  247.             }
  248.         } else   buf[i++] = c;
  249.  
  250.         if (i >= lim) {
  251.             putchar(BELL);
  252.             putchar(BACKSPACE); i--;
  253.         }
  254.  
  255.     }
  256.     buf[i]  = '\0';
  257.     printf("\n");
  258. }
  259.  
  260. /*
  261.  * roomExists()
  262.  *
  263.  * This function returns slot# of named room else ERROR.
  264.  */
  265. int roomExists(room)
  266. char *room;
  267. {
  268.     int i;
  269.  
  270.     for (i = 0;  i < MAXROOMS;  i++) {
  271.         if (
  272.             roomTab[i].rtflags.INUSE == 1   &&
  273.             strCmpU(room, roomTab[i].rtname) == SAMESTRING
  274.         ) {
  275.             return(i);
  276.         }
  277.     }
  278.     return(ERROR);
  279. }
  280.